package com.jiazhong.mybatis.m5;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.mybatis.m5.bean.Emp;
import com.jiazhong.mybatis.m5.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class App {

    private static void a() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.findAll();
        for (Emp emp : emps) {
            System.out.println(emp);
        }
        sqlSession.close();
    }

    private static void b() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.findById(7908);
        System.out.println(emp);
        sqlSession.close();
    }

    private static void c() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.findByName("%李%");
        for (Emp emp : emps) {
            System.out.println(emp);
        }
        sqlSession.close();
    }


    private static void d() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        int page = 1;
        PageHelper.startPage(page, 10);
        List<Emp> emps = empMapper.findAll();
        for (Emp emp : emps) {
            System.out.println(emp);
        }
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);
        System.out.println(pageInfo);
        sqlSession.close();
    }

    private static void e() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        int page = 1;
        PageHelper.startPage(page, 10);
        List<Emp> emps = empMapper.findByEnameAndJob("%张%","程序员");
        for (Emp emp : emps) {
            System.out.println(emp);
        }
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);
        System.out.println(pageInfo);
        sqlSession.close();
    }


    private static void f() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEname("许锦阳");
        emp.setJob("程序员");
        emp.setMgr(8907);
        emp.setSal(9999.9);
        emp.setComm(null);
        emp.setDeptNo(30);
        empMapper.save(emp);
        sqlSession.commit();
        sqlSession.close();
    }
    private static void g() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEname("余江宽");
        emp.setJob("歌手");
        emp.setMgr(7898);
        emp.setSal(99199.9);
        emp.setComm(10.9);
        emp.setDeptNo(40);
        emp.setEmpNo(8041);
        empMapper.update(emp);
        sqlSession.commit();
        sqlSession.close();
    }
    private static void h() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        empMapper.delete(8043);
        sqlSession.commit();
        sqlSession.close();
    }
    public static void main(String[] args) {
        h();
    }
}